package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.NewsListAdapter;
import com.lc.luzhengtang.adapter.NewsListviewAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.NewsBean;
import com.lc.luzhengtang.conn.ConnNewsList;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/3.
 */

public class NewsListActivity2 extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.text_biaoji)
    TextView textBiaoji;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private static final int TOTAL_COUNTER = 18;

    private static final int PAGE_SIZE = 6;

    NewsListAdapter indexAdapter;

    List<NewsBean.DataBeanX.DataBean> mDatas;
    List<String> mDatas2;
    @BindView(R.id.list_notice)
    ListView listNotice;
    @BindView(R.id.text_flag)
    TextView textFlag;
    @BindView(R.id.ck_all)
    CheckBox ckAll;
    @BindView(R.id.layout_select_all)
    LinearLayout layoutSelectAll;
    @BindView(R.id.tv_settlement)
    TextView tvSettlement;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    private int mCurrentCounter = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    NewsListAdapter newsListAdapter;
    public int currentPage = 1;

    NewsListviewAdapter newsListviewAdapter;

    public int Flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newslist);
        ButterKnife.bind(this);
        mDatas = new ArrayList<>();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }


    @Override
    protected void onResume() {
        super.onResume();

        new ConnNewsList(BaseApplication.basePreferences.readUid(), currentPage, new AsyCallBack<NewsBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);

            }

            @Override
            public void onSuccess(String toast, int type, Object o, NewsBean newsBean) throws Exception {
                super.onSuccess(toast, type, o, newsBean);
                mDatas = newsBean.getData().getData();
                newsListviewAdapter = new NewsListviewAdapter(NewsListActivity2.this, Flag, mDatas);
                listNotice.setAdapter(newsListviewAdapter);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);


            }
        }

        ).execute(NewsListActivity2.this, false);
    }

    protected void initData() {
        rvList.setItemAnimator(new DefaultItemAnimator());

        newsListAdapter = new NewsListAdapter(null);
        newsListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        newsListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(NewsListActivity.this, R.drawable.line_divider);
//        rvList.addItemDecoration(new SimpleDividerItemDecoration(NewsListActivity.this, drawable, 1));

        rvList.setAdapter(newsListAdapter);

        swipeLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        swipeLayout.setOnRefreshListener(this);
        newsListAdapter.setOnLoadMoreListener(this);

        setRefreshing(true);
        onRefresh();

    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }


    @Override
    public void onRefresh() {

        new ConnNewsList(BaseApplication.basePreferences.readUid(), currentPage, new AsyCallBack<NewsBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                newsListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, NewsBean newsBean) throws Exception {
                super.onSuccess(toast, type, o, newsBean);
                mDatas = newsBean.getData().getData();
                if (newsBean != null) {
                    currentPage = 1;
                    newsListAdapter.setNewData(mDatas);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);

            }
        }

        ).execute(NewsListActivity2.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnNewsList(BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<NewsBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                newsListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, NewsBean newsBean) throws Exception {
                super.onSuccess(toast, type, o, newsBean);
                mDatas = newsBean.getData().getData();
                if (mDatas != null && mDatas.size() > 0) {
                    currentPage++;
                    indexAdapter.setNewData(mDatas);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    indexAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(NewsListActivity2.this).inflate(R.layout.item_no_data, (ViewGroup) rvList.getParent(), false);
                    indexAdapter.addFooterView(noDataView);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                indexAdapter.setEnableLoadMore(false);
                indexAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
            }
        }

        ).execute(NewsListActivity2.this, false);
    }

    @OnClick({R.id.iv_back, R.id.text_biaoji, R.id.text_flag})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.text_biaoji:

                textFlag.setVisibility(View.GONE);
                textBiaoji.setVisibility(View.VISIBLE);
//                startActivity(new Intent(NewsListActivity.this, EditNewsListActivity.class));
                Flag = 1;
                new ConnNewsList(BaseApplication.basePreferences.readUid(), currentPage, new AsyCallBack<NewsBean>() {
                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);

                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, NewsBean newsBean) throws Exception {
                        super.onSuccess(toast, type, o, newsBean);
                        mDatas = newsBean.getData().getData();
                        newsListviewAdapter = new NewsListviewAdapter(NewsListActivity2.this, Flag, mDatas);
                        listNotice.setAdapter(newsListviewAdapter);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);


                    }
                }

                ).execute(NewsListActivity2.this, false);
                newsListAdapter.notifyDataSetChanged();
                break;

            case R.id.text_flag:


                break;

        }
    }

}
